package a07_二叉树;

/**
 * <p>
 * a17_路径总和复习0
 * </p>
 *
 * @author flyduck
 * @since 2025/1/6
 */
public class a17_路径总和复习0 {

    public boolean hasPathSum(TreeNode root, int targetSum) {
        return traversal(root, targetSum, 0);
    }

    private boolean traversal(TreeNode currentNode, int targetSum,int sum){
        if (currentNode == null) {
            return false;
        }
        sum = sum + currentNode.val;
        if(currentNode.left == null && currentNode.right == null){
            return targetSum == sum;
        }
        boolean leftSuccess = false;
        if (currentNode.left != null) {
            leftSuccess = traversal(currentNode.left, targetSum, sum);
        }

        boolean rightSuccess = false;
        if (currentNode.right != null) {
            rightSuccess = traversal(currentNode.right, targetSum, sum);
        }
        return leftSuccess || rightSuccess;
    }
}
